'use strict';
const ulid = require('../public/ulid');
const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller tr069Controller 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/

class Tr069Controller extends Controller {
 async nameChange(){

    const data = this.ctx.request.body
    console.log(data,'data')
  try{
   await this.ctx.service.tr069.findOltType(data)
  }catch(err){
    this.ctx.body = {
       flag:false,
       err
    }
    return 
  }
  await this.ctx.model.OnuList.updateOne({sn:data.SN,onlyindex:val},{
    $set:{
       ONUname:data.newName
    }
  })
  this.ctx.body = {
    flag:true,

  }
  
  }
  mock() {
    const result = [];
    for (let i = 0; i < 1; i++) {
      result.push({
        id: ulid.ulid(),
        onlyindex: i.toString(),
        PONindex: i.toString(),
        ONUindex: i.toString(),
        ONUname: 'test' + i,
        state: i % 2 === 0 ? 'offline' : 'online',
        onuprofile: '',
        mode: '',
        info: '',
        user: '',
        TotalTCONTnumber: '',
        catvstate: false,
        Powerfeedvoltage: '',
        Laserbiascurrent: '',
        Temperature: '',
        Description: '',
        Mainsoftwareversion: '',
        Standbysoftwareversion: '',
        VendorID: '',
        Version: '',
        SN: 'ASTCA1050060',
        Operatestatus: '',
        EquipmentID: '',
        OMCCVersion: '',
        Securitycapability: '',
        Model: '',
        Survivaltime: '',
        TotalGemPortNum: '',
        SysUpTime: '',
        TotalUNInumber: '',
        EthernetUNInumber: '',
        TenGEnumber: '',
        GEnumber: '',
        FEnumber: '',
        CESUNInumber: '',
        POTSUNInumber: '',
        VideoUNInumber: '',
        WIFIUNInumber: '',
        XDSLUNInumber: '',
        IPhostnumber: '',
        IPv6hostnumber: '',
        VEIPnumber: '',
        OperationId: '',
        CTCspcversion: '',
        CUCspcversion: '',
        ONUtype: '',
        Txpowersupplycontrol: '',
        updatetime: '',
        IP: '',
        IPstate: '',
        speedlimit: '',
        vlan: 1,
        TcountName: '',
        TrafficName: '',
        ZTEMode: '',
        is_test: false,
        tid: ulid.ulid(),
        S: 1,
        P: 1,
        mac: '',
        serviceport: '',
        OUI: '940268',
        deviceType: 'COMMON 4+1 AP',
        provider: 'huawei', // 供应商
        function: '1电话、4GE LAN、2.4G WIFI',
        hardwareVersion: '1007.A',
        softwareVersion: 'V3R017C10S138',
        ontSN: '48575443150AB49C',
        remoteAddress: 'http://10.10.1.121:9090',
        reportInterval: '60',
        memo: '华为',
        lanIP: '192.168.1.1',
        terminalBand: 'Huawei Technologies Co., Ltd',
        terminalModel: 'EG8141A5',
        LOID: '',
        ponType: 'GPON',
        RXpwr: '-26',
        TXpwr: '2',
        electric: '7',
        voltage: '3322',
        temperature: '30',
        wanlinks: '2', // wan连接数
        wifiState: 'HW3000',
        signalPath: '3', // 信道数
        voiceLine: '线路1',
        runningTime: '0天3小时37分16秒',
        createdAt: '2020-12-18 15:16:40',
        lastReportTime: '2020-12-22 15:29:49',
        reportIP: '10.30.10.20',
        onlineState: '',
        configSendState: '',
      });
    }
    const a = Object.create(null);
    result.forEach(item => {
      a[item.id] = item;
    });
    return a;
  }
  async list() {
    /**
        * @summary onu列表分页。
        * @description onu列表分页。
        * @router get /tr069/onulist
        * @request query integer limit
        * @request query integer page
        * @request query String condition
        * @request query String onlineState
        * @request query String configSendState
        * @request query String group
        * @request query String order
        * @request query String sort
        */
    console.log(this.ctx.query);
    // const data = this.mock();
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.tr069List(params);
    console.log(result);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result.rows,
      count: result.count,
    };
  }

  async getOnlineStates() {
    /**
        * @summary getOnlineStates
        * @description getOnlineStates onu在线状态下拉框数据
        * @router get /tr069/getOnlineStates
        */
    this.ctx.body = {
      msg: '',
      status: 200,
      data: [{ id: 'Connected', name: 'Connected' },
        { id: 'OffLine', name: 'OffLine' },
        { id: 'LOS', name: 'LOS' },
        { id: 'working', name: 'working' }],
    };
  }

  async getSendStates() {
    /**
        * @summary getOnlineStates
        * @description getOnlineStates onu下发状态下拉框数据
        * @router get /tr069/getSendStates
        */
    this.ctx.body = {
      msg: '',
      status: 200,
      data: [{ id: 0, name: 'send' },
        { id: 1, name: 'not send' }],
    };
  }
  async getGroups() {
    /**
    * @summary getGroups
    * @description getGroups onu分组下拉框数据
    * @router get /tr069/getGroups
    */
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    const result = await this.ctx.service.tr069.getGroups(tid);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result,
    };
  }
  async getFirmwareVersions() {
    /**
    * @summary getFirmwareVersions
    * @description getFirmwareVersions onu固件版本下拉框数据
    * @router get /tr069/getFirmwareVersions
    */
    this.ctx.body = {
      msg: '',
      status: 200,
      data: [{ id: 0, name: 'v1.0' },
        { id: 1, name: 'v2.0' }],
    };
  }
  async getVoiceStates() {
    /**
    * @summary getVoiceStates
    * @description getVoiceStates onu语音状态下拉框数据
    * @router get /tr069/getVoiceStates
    */
    this.ctx.body = {
      msg: '',
      status: 200,
      data: [{ id: 0, name: 'normal' },
        { id: 1, name: 'unregist' },
        { id: 2, name: 'error' }],
    };
  }

  async refresh() {
    /**
    * @summary refresh
    * @description refresh onu刷新
    * @router get /tr069/refresh
    */
    this.ctx.body = {
      msg: '',
      status: 200,
      data: [],
    };
  }
  async getConfigTemplateList() {
    /**
    * @summary getConfigTemplateList
    * @description getConfigTemplateList 配置模板列表
    * @router get /tr069/getConfigTemplateList
    */
    try {
      const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
      const result = await this.ctx.service.tr069.list(tid);
      this.ctx.body = {
        msg: 'success',
        status: 200,
        data: result,
      };
    } catch (error) {
      this.ctx.body = {
        msg: 'error',
        status: 200,
        data: [],
      };

    }
  }

  async createGroup() {
    /**
       * @summary createGroup
       * @description 创建分组
       * @router post /tr069/createGroup
       * @request body createGroup value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.createGroup(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }

  async deleteGroup() {
    /**
       * @summary createGroup
       * @description 创建分组
       * @router post /tr069/deleteGroup
       * @request body deleteGroup value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.deleteGroup(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }

  async addGroup() {
    /**
       * @summary addGroup
       * @description 将onu加入分组中
       * @router post /tr069/addGroup
       * @request body group_onu value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.addGroup(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }

  async subGroup() {
    /**
       * @summary subGroup
       * @description 将onu从分组中移除
       * @router post /tr069/addGroup
       * @request body group_onu value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.subGroup(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }

  async addConfigTemplate() {
    /**
       * @summary addConfigTemplate
       * @description 添加配置模板
       * @router post /tr069/addConfigTemplate
       * @request body addConfigTemplate value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.addConfigTemplate(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }

  async updateConfigTemplate() {
    /**
       * @summary addConfigTemplate
       * @description 更新配置模板
       * @router post /tr069/updateConfigTemplate
       * @request body addConfigTemplate value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.updateConfigTemplate(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }
  async modifyConfigTemplates() {
    /**
       * @summary modifyConfigTemplates
       * @description 批量修改配置模板
       * @router post /tr069/modifyConfigTemplates
       * @request body modifyConfigTemplateParams value 传入参数
       */
    const params = this.ctx.request.body;
    const result = await this.ctx.service.tr069.modifyConfigTemplates(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }
  async sendTemplate() {
    /**
     * @summary sendTemplate
     * @description 生成发送模板的内容
     * @router post /tr069/sendTemplate
     * @request body sendTemplate
     */
    const params = this.ctx.request.body;
    const result = await this.ctx.service.tr069.sendTemplate(params);
    this.ctx.body = {
      msg: 'template will send',
      status: 200,
      data: result,
    };
  }
  async modifyBusinessTemplates() {
    /**
       * @summary modifyBusinessTemplates
       * @description 批量修改业务配置模板
       * @router post /tr069/modifyBusinessTemplates
       * @request body modifyConfigTemplateParams value 传入参数
       */
    const params = this.ctx.request.body;
    const result = await this.ctx.service.tr069.modifyBusinessTemplates(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }


  async getOnuWanInfo() {
    /**
    * @summary getOnuWanInfo
    * @description getOnuWanInfo 通过tr069获取onu的wan
    * @router get /tr069/getOnuWanInfo
    * @request query string id
    */
    console.log(this.ctx.query);
    const result = {
      status: 200,
      wans: [
        {
          address_type: 'DHCP',
          bridge_ports: 'LAN1,LAN2,LAN3,LAN4,SSID1',
          dns: '211.140.13.188,211.140.188.188',
          enable: 'yes',
          enable_nat: 'yes',
          gateway: '10.30.10.1',
          id: '1',
          ip: '10.30.10.38',
          link_mode: 'IP_Routed',
          mac: 'F8:98:EF:08:75:E2',
          name: '1_TR069_INTERNET_R_VID_1030',
          netmask: '255.255.255.0',
          prefix: 'WANDevice.1.WANConnectionDevice.1.WANIPConnection.1',
          service: 'TR069_INTERNET',
          status: 'Connected',
          uptime: '917',
        },
        {
          address_type: 'PPPoE',
          bridge_ports: '',
          dns: null,
          enable: 'yes',
          enable_nat: 'yes',
          gateway: null,
          id: '2',
          ip: '0.0.0.0',
          link_mode: 'IP_Routed',
          mac: 'F8:98:EF:08:75:E3',
          name: '2_OTHER_R_VID_1040',
          netmask: null,
          prefix: 'WANDevice.1.WANConnectionDevice.2.WANPPPConnection.1',
          service: 'OTHER',
          status: 'Disconnected',
          uptime: '0',
          username: 'bbb',
        },
      ],
    };
    this.ctx.body = result;
  }

  async getBusinessTemplateList() {
    /**
       * @summary getBusinessTemplateList
       * @description 获取业务模板列表
       * @router get /tr069/getBusinessTemplateList
       */
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    const result = await this.ctx.service.tr069.getBusinessTemplateList(tid);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: result,
    };
  }
  async getBusinessTemplates() {
    /**
       * @summary getBusinessTemplates
       * @description 获取业务模板下拉框内容
       * @router get /tr069/getBusinessTemplates
       */
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    const result = await this.ctx.service.tr069.getBusinessTemplates(tid);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: result,
    };
  }

  async createBusinessTemplate() {
    /**
       * @summary createBusinessTemplate
       * @description 创建业务模板
       * @router post /tr069/createBusinessTemplate
       * @request body BusinessTemplate value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.createBusinessTemplate(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }
  async updateBusinessTemplate() {
    /**
       * @summary updateBusinessTemplate
       * @description 创建业务模板
       * @router post /tr069/updateBusinessTemplate
       * @request body BusinessTemplate value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.updateBusinessTemplate(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }
  async createDefaultTemplates() {
    /**
       * @summary createDefaultTemplates
       * @description 创建默认模板
       * @router post /tr069/createDefaultTemplates
       * @request body createDefaultTemplates value 传入参数
       */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.createDefaultTemplates(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }
  async updateONUInfos() {
    /**
       * @summary updateONUInfos
       * @description 更新onu的在线状态、oui、mac、终端厂商、终端型号
       * @router post /tr069/updateONUInfos
       * @request body updateONUInfos value 传入参数
       */
    const params = this.ctx.request.body;
    const result = await this.ctx.service.tr069.updateONUInfos(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: '',
    };
  }
  async getAllOnus() {
    /**
     * @summary getAllOnus
     * @description 查询数据库中所有的onu信息
     * @router get /tr069/getAllOnus
     */
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    const result = await this.ctx.service.tr069.getAllOnus(tid);
    this.ctx.body = {
      msg: 'success',
      status: 200,
      data: result,
      count: 20,
    };
  }

  async updateAllOnus() {
    /**
     * @summary updateAllOnus
     * @description 更新onu表可以支持acs的设备的acsSupport状态
     * @router post /tr069/updateONUInfos
     * @request body updateAllOnus value 传入参数
     */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.tr069.updateAllOnus(params);
    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: '',
    };
  }
}

module.exports = Tr069Controller;
